layui.use(['form','laytpl'], function () {
    var form = layui.form,
        layer = layui.layer,
        laytpl = layui.laytpl;
        $ = layui.$;

    //班级下拉选项
    let classInfo;
    getAjax(true, "/api/stu-class/search/list", '',function (res) {
        if(res.code == 0){
            if(res.data != null){
                classInfo = res.data;
                let htmlStr = '<option value = "">请选择</option>'
                for(let row in res.data){
                    var classNameDesc = '';
                    if(res.data[row].grade && res.data[row].stuClass){
                        classNameDesc = '（'+res.data[row].grade+'年级'+res.data[row].stuClass+'班'+'）';
                    }
                    htmlStr += '<option value = "' + res.data[row].id + '">'+res.data[row].className + classNameDesc +'</option>'

                    //班级提示信息
                    if(getJsonRequest("classId") && getJsonRequest("classId").classId == res.data[row].id){
                        dataEngine(laytpl,'stu-class-info','class-top', res.data[row]);
                    }
                }
                $("#stu-class-sel").html(htmlStr);
                form.render('select', 'student-info');

                //表单数据回显，需要下垃框渲染完成后再回显数据，否则会出现回显失败概率事件
                let studentInfo;
                let idParam = getJsonRequest("id");
                if(!isEmpty(idParam)){
                    getAjax(false,"/api/student/get",idParam, function (res) {
                        if(res.code == 0){
                            if(res.data != null){
                                studentInfo = res.data;
                                form.val("student-info", studentInfo);
                            }
                        }else {
                            let index = layer.alert(res.msg, {title: '提示', closeBtn: 0}, function () {
                                layer.close(index);
                                let iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });
                            return false;
                        }
                    });
                }
            }
        }else {
            let index = layer.alert(res.msg, {title: '提示', closeBtn: 0}, function () {
                layer.close(index);
                let iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
            });
            return false;
        }
    });

    //下拉框选中
    form.on('select(stu-class)', function(e){
        let element = classInfo.filter(item => item.id == e.value)[0];
        if(!element){
            $(".class-top-body").remove();
        }else {
            //班级提示信息
            dataEngine(laytpl,'stu-class-info','class-top', element);
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        postAjax("/api/student/edit", data.field, function (res) {
            if(res.code == 0){
                let index = layer.alert("保存成功", { title: '提示', closeBtn: 0 }, function () {
                    layer.close(index);
                    let iframeIndex = parent.layer.getFrameIndex(window.name);
                    console.log(window.parent.reload())
                    parent.layer.close(iframeIndex);
                });
            }else {
                layer.alert(res.msg, { title: '提示' });
            }
        })
        return false;
    });
});